//
//  SEGIntegratedServicesApplyViewModel.m
//  SEGViewModel
//
//  Created by 梁学彰 on 2020/11/26.
//

#import "SEGIntegratedServicesApplyViewModel.h"
#import "SEGIntegratedServicesManager.h"

@implementation SEGIntegratedServicesApplyViewModel

- (RACSignal *)requestFromData {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        [SEGIntegratedServicesManager requestIntegratedServiceFromWithFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if (!isSuccessed) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                return;
            }
            NSMutableArray<SEGIntegratedServicesApplyFromModel *> *tempArray = [NSMutableArray array];
            for (NSDictionary *subDic in result[@"data"][@"formFieldList"]) {
                SEGIntegratedServicesApplyFromModel *model = [SEGIntegratedServicesApplyFromModel yy_modelWithDictionary:subDic];
                [tempArray appendObject:model];
            }
            self.formList = tempArray;
            [subscriber sendCompleted];
        }];
        
        
        return nil;
    }];
    return signal;
}


- (RACSignal *)requestApplyIntegratedService {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        // 2.接口调用
        
        [SEGIntegratedServicesManager requestApplyIntegratedServiceWithFormList:self.formList serviceListModel:self.model finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            
            if (!isSuccessed) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                return;
            }
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
    return signal;
}

@end
